<template>
  <el-form id="loginFrom" ref="loginFormRef" :model="loginForm" :rules="rules">
    <h3 style="color: dodgerblue;font-family: 'Algerian'">Login</h3>
    <!--    用户名-->
    <el-form-item prop="username">
      <el-input v-model="loginForm.username" auto-complete="false" placeholder="请输入用户名"
                prefix-icon="el-icon-user"></el-input>
    </el-form-item>
    <!--    密码-->
    <el-form-item prop="password">
      <el-input v-model="loginForm.password" auto-complete="false" prefix-icon="el-icon-lock" show-password></el-input>
    </el-form-item>
    <!--    验证码-->
    <el-form-item prop="code">
      <el-input v-model="loginForm.code" auto-complete="false" placeholder="点击图片,更换验证码"
                style="width: 70%;float: left" @keyup.enter.native="login"></el-input>
      <img :src="codeImgUrl" alt="" style="float:left;width: 30%;" @click="getCode">
    </el-form-item>
    <el-form-item>
      <el-button style="float: right" type="primary" @click="login">登录</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import personal from '@/utils/personal'
import router from '@/router'

export default {
  name: 'Login',
  data () {
    return {
      checked: false,
      codeImgUrl: 'http://localhost:88/code/getVerify?time=' + new Date(),
      loginForm: {
        username: '',
        password: '',
        code: ''
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名!', trigger: 'blur' },
          { min: 3, max: 10, message: '长度在3到10个字符之间', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码!', trigger: 'blur' },
          { min: 6, max: 15, message: '长度在6到15个字符之间', trigger: 'blur' }
        ],
        code: [
          { required: true, message: '请输入验证码!', trigger: 'blur' }
        ]
      }
    }
  },
  beforeRouteEnter (to, from, next) {
    if (sessionStorage.getItem('token') ===
      null &&
      sessionStorage.getItem('user') === null) { // 权限控制的具体规则
      next()
    } else {
      alert('不能重复登入!')
      router.push('Home')
    }
  },
  methods: {
    getCode () {
      this.codeImgUrl = 'http://localhost:88/code/getVerify?time=' + new Date()
    },
    login () {
      this.$refs.loginFormRef.validate((valid) => {
        if (valid) {
          // personal.login(this.loginForm).then(this.getCode)
          personal.checked(this.loginForm.code).then(res => {
            if (res.code !== 200) {
              this.getCode()
              this.$message.error(res.msg)
            } else {
              sessionStorage.clear()
              personal.login(this.loginForm).then(this.getCode)
            }
          })
        } else {
          this.getCode()
          this.$message.error('错了哦，这是一条错误消息')
          return false
        }
      })
    }
  }
}
</script>

<style scoped>
#loginFrom {
  width: 350px;
  padding: 20px;
  height: 300px;
  text-align: center;
  border: 1px solid #eee;
  border-radius: 15px;
  background-clip: padding-box;
  box-shadow: 0 0 25px #cac6c6;
  margin: 200px auto;
}

</style>
